package cn.starrysky108.simpleerp.core.config;

import cn.starrysky108.simpleerp.core.properties.SimpleErpProperties;
import cn.starrysky108.simpleerp.core.util.UploadUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Carl Lee
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    /**
     * 文件上传路径
     */
    private final String uploadDir;

    public WebMvcConfig(SimpleErpProperties properties) {
        //文件路径后必须添加/或者\\。否则会导致无法映射
        String path = properties.getUploadDir() + "/";
        //规范化路径
        this.uploadDir = UploadUtil.normalize(path);
    }

    /**
     * 配置静态资源处理器
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/uploads/**").addResourceLocations("file:" + uploadDir);
    }
}

